/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "devInfo.h"
#include "mnt.h"
#include "bleMaster.h"
#include "protocol.h"


#define DBG_LEVEL DBG_INFO
#define DBG_SECTION_NAME  "main.c"
#include <rtdbg.h>

#define WDT_DEVICE_NAME   "wdt"    /* 看门狗设备名称 */


static void idle_hook(void)
{
		rt_device_t wdg_dev; 				/* 看门狗设备句柄 */

    /* 根据设备名称查找看门狗设备，获取设备句柄 */
    wdg_dev = rt_device_find(WDT_DEVICE_NAME);
    /* 在空闲线程的回调函数里喂狗 */
    rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
    //rt_kprintf("feed the dog!\n ");
}


static int wdt_process(rt_uint32_t timeout)
{
    rt_err_t ret = RT_EOK;
		rt_device_t wdg_dev; 				/* 看门狗设备句柄 */

    /* 根据设备名称查找看门狗设备，获取设备句柄 */
    wdg_dev = rt_device_find(WDT_DEVICE_NAME);
    if (!wdg_dev)
    {
        rt_kprintf("find %s failed!\n", WDT_DEVICE_NAME);
        return RT_ERROR;
    }
		rt_device_open(wdg_dev,RT_DEVICE_OFLAG_OPEN);
    /* 设置看门狗溢出时间 */
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);//
    if (ret != RT_EOK)
    {
        rt_kprintf("set %s timeout failed!\n", WDT_DEVICE_NAME);
        return RT_ERROR;
    }
    /* 启动看门狗 */
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
    if (ret != RT_EOK)
    {
        rt_kprintf("start %s failed!\n", WDT_DEVICE_NAME);
        return -RT_ERROR;
    }
    /* 设置空闲线程回调函数 */
    rt_thread_idle_sethook(idle_hook);
		rt_kprintf("wdt set finsh\r\n");

    return ret;
}



int main(void)
{	
	rt_kprintf("system clock freq = %d\r\n",HAL_RCC_GetSysClockFreq());

	wdt_process(3);
	//powerManage_init();//电源管理初始化
	mnt_lfs_int();
  flash_load_bleInfo();
  pc_proto_init();
  adv_master_init();


  while (1)
  {
	
  	rt_thread_mdelay(100);
  }

  return RT_EOK;
}
